Conversation
Adds a new Developer Tutorials section with three tutorials: - Adding a New Modifier: covers the modifier lifecycle, Pydantic params, HooksMixin, and a concrete WeightClampModifier example - Adding a New Observer: covers the Observer contract, stateful observers, and a PercentileObserver example with recipe usage - Adding MoE Model Support: covers MoECalibrationModule, the calibration forward pattern, and a step-by-step guide referencing the GLM-4.7 example Also adds the section to .nav.yml between User Guides and Examples. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
|
👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review. Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant enhancement to the project's documentation by adding a dedicated "Developer Guides" section. This new section provides essential resources for contributors looking to extend the LLM Compressor framework, covering key areas such as implementing custom modifiers, creating new observers for quantization, and integrating support for Mixture of Experts (MoE) models. The guides offer in-depth explanations of core contracts, lifecycles, and practical examples to facilitate easier and more effective contributions. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
…ew Model" Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request introduces new developer tutorials for LLM Compressor, covering how to add custom Modifiers, Observers, and support for MoE models. The documentation updates include new markdown files and navigation links. Review feedback suggests improving the clarity of lifecycle hook descriptions in the modifier tutorial, defining the ds variable in the MoE example, and optimizing the get_min_max implementation in the observer tutorial for efficiency and robustness.
Replaces the GLM-4.7 reference with the Llama4 implementation as the canonical example. Updates the forward pattern, contract section, and step-by-step to reflect Llama4-specific details including packed expert unpacking, is_permanent=True, shared_expert handling, and router score application to expert outputs rather than inputs. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…e/zp Clarifies that observers return min and max values, which are then passed to calculate_qparams and generate_gparam in compressed-tensors to produce scale and zero_point. Adds a section explaining how the base class uses observer output, and updates the example docstring and tips accordingly. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…vers - Correct: observers compute min/max values, not scale/zero_point directly; compressed-tensors calculate_qparams/generate_gparam handle that conversion - Fix base class description: correct method names (get_min_max, get_global_min_max) and accurate description of what the base class handles - Document all observer variants: memoryless_minmax, static_minmax, minmax, memoryless_mse, mse (previously only MinMax and MSE were mentioned) - Split observer_kwargs table by observer type with accurate defaults Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
kylesayrs
left a comment
There was a problem hiding this comment.
Reviewed the add-modifier section. Overall looks good
I think the only question we need to answer is: How much legacy abstraction do we want to expose to the user? Events? Training events like BATCH_START? Does State belong here or somewhere else?
| @@ -0,0 +1,177 @@ | |||
| # Adding a New Modifier | |||
|
|
|||
| Modifiers are the core extension point in LLM Compressor. Each compression algorithm — GPTQ, AWQ, SmoothQuant, and others — is implemented as a modifier. This tutorial walks through the modifier contract, lifecycle, and how to implement a custom one. | |||
There was a problem hiding this comment.
| Modifiers are the core extension point in LLM Compressor. Each compression algorithm — GPTQ, AWQ, SmoothQuant, and others — is implemented as a modifier. This tutorial walks through the modifier contract, lifecycle, and how to implement a custom one. | |
| Modifiers are the core extension point in LLM Compressor. Each compression algorithm — GPTQ, AWQ, SmoothQuant, and others — is implemented as a modifier. This tutorial walks through the modifier contract, lifecycle, and how to implement a custom modifier. |
|
|
||
| ## What is a Modifier? | ||
|
|
||
| A modifier is a Pydantic model that hooks into the compression pipeline at well-defined lifecycle points. When you call `oneshot`, LLM Compressor: |
There was a problem hiding this comment.
| A modifier is a Pydantic model that hooks into the compression pipeline at well-defined lifecycle points. When you call `oneshot`, LLM Compressor: | |
| A modifier is a subclass of the `Modifier` base class that hooks into the compression pipeline at well-defined lifecycle points. When you call `oneshot`, LLM Compressor will: |
There was a problem hiding this comment.
The fact that it's a Pydantic model is more of a technical detail, not core to the identity of a modifier.
| 1. Instantiates modifiers from the recipe | ||
| 2. Calls `initialize` on each modifier | ||
| 3. Runs calibration batches, firing `Event`s that modifiers respond to | ||
| 4. Calls `finalize` on each modifier |
There was a problem hiding this comment.
I worry that the lack of precision here might be misleading. The most precise description would be something like this:
| 1. Instantiates modifiers from the recipe | |
| 2. Calls `initialize` on each modifier | |
| 3. Runs calibration batches, firing `Event`s that modifiers respond to | |
| 4. Calls `finalize` on each modifier | |
| * Instantiate modifiers from the recipe | |
| * Call `on_initialize` on each modifier | |
| For each pipeline: | |
| * Dispatch the model, then call `on_start` for each modifier in the pipeline | |
| * Calibrate the model with calibration data, triggering the `calibration_epoch_start`, `sequential_epoch_end`, and `calibration_epoch_end` events for each modifier in the pipeline | |
| * Call `on_end` on each modifier in the pipeline | |
| * Call `on_finalize` on each modifier once all pipelines have finished |
I think this level of detail would be fine so long as the formatting looks okay
| 3. Runs calibration batches, firing `Event`s that modifiers respond to | ||
| 4. Calls `finalize` on each modifier | ||
|
|
||
| Modifiers express what they want to do at each stage by overriding lifecycle hooks. |
There was a problem hiding this comment.
| Modifiers express what they want to do at each stage by overriding lifecycle hooks. | |
| Modifiers express what they want to do at each stage by implementing lifecycle hooks. |
| | Hook | Called by | | ||
| |------|-----------| | ||
| | `on_initialize` | `CompressionLifecycle.initialize()` | | ||
| | `on_event` / `on_start` / `on_update` / `on_end` | `CompressionLifecycle.event()` → `Modifier.update_event()` | |
There was a problem hiding this comment.
I think that CompressionLifecycle events are confusing and not useful as a concept for users. They're this weird middleware between LifecycleCallbacks and modifier event implementations, and something that I hope we can get rid of at some point.
|
|
||
| ## Example: A Weight-Clamping Modifier | ||
|
|
||
| The following modifier clamps the stored weight tensors of all `Linear` layers to a fixed absolute magnitude. It follows the real modifier pattern by handling both pipelines in `on_event`: when using the sequential pipeline it clamps weights layer-by-layer as each subgraph completes (`SEQUENTIAL_EPOCH_END`), and when using the basic pipeline it clamps all weights at once at the end of calibration (`CALIBRATION_EPOCH_END`). |
There was a problem hiding this comment.
| The following modifier clamps the stored weight tensors of all `Linear` layers to a fixed absolute magnitude. It follows the real modifier pattern by handling both pipelines in `on_event`: when using the sequential pipeline it clamps weights layer-by-layer as each subgraph completes (`SEQUENTIAL_EPOCH_END`), and when using the basic pipeline it clamps all weights at once at the end of calibration (`CALIBRATION_EPOCH_END`). | |
| The following modifier clamps the stored weight tensors of all `Linear` layers to a fixed absolute magnitude. When using the sequential pipeline it clamps weights layer-by-layer as each subgraph completes (`SEQUENTIAL_EPOCH_END`), and when using the basic pipeline it clamps all weights at once at the end of calibration (`CALIBRATION_EPOCH_END`). |
| """ | ||
|
|
||
| max_weight_magnitude: float = 1.0 | ||
| targets: list[str] = ["Linear"] |
There was a problem hiding this comment.
| targets: list[str] = ["Linear"] | |
| targets: list[str] = field(default_factory=lambda: ["Linear"]) |
It is bad practice to declare mutable objects as default values
| max_weight_magnitude: float = 1.0 | ||
| targets: list[str] = ["Linear"] | ||
| ignore: list[str] = [] | ||
|
|
There was a problem hiding this comment.
| _clamped: set[str] = PrivateAttr(default_factory=set) |
| f"No modules matched targets={self.targets} ignore={self.ignore}" | ||
| ) | ||
|
|
||
| self._clamped: set[str] = set() |
There was a problem hiding this comment.
| self._clamped: set[str] = set() |
| from llmcompressor import oneshot | ||
|
|
||
| model = AutoModelForCausalLM.from_pretrained("your-model") | ||
| tokenizer = AutoTokenizer.from_pretrained("your-model") |
There was a problem hiding this comment.
As a matter of preference, I think "your-model" is less distracting than a real model name like "Qwen/Qwen3-0.6B"
Summary
Adds a new Developer Guides section to the docs for contributors extending LLM Compressor:
on_initialize,on_start,on_update,on_end,on_finalize), Pydantic parameter declaration,HooksMixinusage, and a concreteWeightClampModifierwalkthroughcompressed-tensorscalculate_qparamsandgenerate_gparamconsume those values, covers theObservercontract, stateful observers, and aPercentileObserverexample with recipe usageMoECalibrationModulecontract, and a step-by-step guide usingSequentialLlama4TextMoeas the canonical reference. Covers Llama4-specific patterns: packed expert unpacking via a helperModuleList,is_permanent=Truefor vLLM compatibility,shared_experthandling, and applying routing scores to expert outputs (not inputs) to avoid NaNs during calibrationAlso updates
.nav.ymlto add the new section between User Guides and Examples.Updates the existing User Guides:
guides/observers.md) — corrects observer descriptions (min/max values, not scale/zero_point), fixes base class method names, documents all previously undocumented observer variants (memoryless_minmax,static_minmax,memoryless_mse), and splitsobserver_kwargstable by observer type with accurate defaults🤖 Generated with Claude Code